package com.xx.sg.controller;

import com.xx.sg.own.entity.FacilityTxt;
import com.xx.sg.own.service.FacilityTxtService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/data")
@Tag(name = "数据专栏", description = "数据专栏API")
public class FacilityTxtController {

    @Autowired
    private FacilityTxtService facilityTxtService;

    @GetMapping("/info")
    @Operation(summary = "查询数据", description = "查询数据")
    public Map info(String id) {

        FacilityTxt txt = facilityTxtService.getById(id);
        if (txt == null) {
            txt = new FacilityTxt();
        }
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("data", txt);
        res.put("message", "成功");
        return res;
    }

    @PostMapping("/modify")
    @Operation(summary = "修改数据", description = "修改数据")
    public Map modify(FacilityTxt facilityTxt) {

        FacilityTxt txt = facilityTxtService.getById(facilityTxt.getId());
        if (txt == null) {
            facilityTxtService.save(facilityTxt);
        } else {
            facilityTxtService.updateById(facilityTxt);
        }

        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("data", true);
        res.put("message", "成功");
        return res;
    }
}
